package main

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"strconv"
)

// GetAxis col 列 如： A B C D
// raw 行 如：1 2 3 4
func GetAxis(col string, raw int) string {
	return col + strconv.Itoa(raw)
}

func SaveToExcel(file string, books []*BookItem) error {
	f := excelize.NewFile()
	// 标题 头
	f.SetCellValue("Sheet1", GetAxis("A", 1), "序号")
	f.SetCellValue("Sheet1", GetAxis("B", 1), "书号")
	f.SetCellValue("Sheet1", GetAxis("C", 1), "书名")
	f.SetCellValue("Sheet1", GetAxis("D", 1), "作者")
	f.SetCellValue("Sheet1", GetAxis("E", 1), "出版社")
	f.SetCellValue("Sheet1", GetAxis("F", 1), "出版日期")
	f.SetCellValue("Sheet1", GetAxis("G", 1), "定价")
	f.SetCellValue("Sheet1", GetAxis("H", 1), "数量")
	f.SetCellValue("Sheet1", GetAxis("I", 1), "图书分类号")
	f.SetCellValue("Sheet1", GetAxis("J", 1), "审核类别")
	// 写入内容
	for i, book := range books {
		index := i + 2
		f.SetCellValue("Sheet1", GetAxis("A", index), book.Index)
		f.SetCellValue("Sheet1", GetAxis("B", index), book.Isbn)
		f.SetCellValue("Sheet1", GetAxis("C", index), book.Title)
		f.SetCellValue("Sheet1", GetAxis("D", index), book.AuthorName)
		f.SetCellValue("Sheet1", GetAxis("E", index), book.Publisher)
		f.SetCellValue("Sheet1", GetAxis("F", index), book.Published)
		f.SetCellValue("Sheet1", GetAxis("G", index), book.Price)
		f.SetCellValue("Sheet1", GetAxis("H", index), book.Count)
		f.SetCellValue("Sheet1", GetAxis("I", index), "")
		f.SetCellValue("Sheet1", GetAxis("J", index), "")
	}
	// Set active sheet of the workbook.
	err := f.SaveAs(file)
	if err != nil {
		return err
	}
	return nil
}
